﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.UserControl
{
    public partial class Department : System.Web.UI.UserControl
    {
        private IDepartmentBLL departmentBLL = IocContainer.Instance.Resolve<IDepartmentBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                this.litTreeView.Text = BuildTree();
            }
        }

        private string BuildTree()
        {
            DataTable dtDepartment = departmentBLL.GetAllDepartment();

            return GenerateTreeHtmlFragment(dtDepartment, 0);
        }

        private string GenerateTreeHtmlFragment(DataTable dtDepartment, int parentId)
        {
            StringBuilder result = new StringBuilder("");
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                DataRow[] drRoots = dtDepartment.Select("ParentId = " + parentId);
                foreach (DataRow row in drRoots)
                {
                    result.AppendFormat("<ul><li><a href=\"javascript:void(0)\" title=\"{0}\" tname=\"{1}\" tvalue=\"{2}\" tlevel=\"{4}\">{3}</a>", row["Description"], row["ParentId"], row["DepartmentId"], row["DepartmentName"], row["Level"]);

                    int deptId = 0;
                    int.TryParse(row["DepartmentId"].ToString(), out deptId);
                    result.Append(GenerateTreeHtmlFragment(dtDepartment, deptId));
                    result.Append("</li></ul>");
                }
            }
            return result.ToString();
        }

        public void ReBuildTree()
        {
            this.litTreeView.Text = BuildTree();
        }

        public int SelectedNodeId
        {
            get
            {
                int result = 0;
                int.TryParse(this.txtHidDeptId.Value, out result);
                return result;
            }
        }

        public int SelectedNodeParentId
        {
            get
            {
                int result = 0;
                int.TryParse(this.txtHidParentId.Value, out result);
                return result;
            }
        }

        public int SelectedNodeLevel
        {
            get
            {
                int result = 0;
                int.TryParse(this.txtHidLevel.Value, out result);
                return result;
            }
        }

        public Boolean IsAllowPostBack
        {
            get; set;
        }


        public delegate void EventHandler(object sender, EventArgs e);
        public event EventHandler SelectedChange;
        protected void btnDelegate_Click(object sender, EventArgs e)
        {
            SelectedChange(sender,e);
        }
    }
}